package com.neusoft.neuiotms.dcom.controller;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.MaintainPlanDTO;
import com.neusoft.neuiotms.dcom.service.MaintainPlanService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "维护计划管理")
@RestController
@RequestMapping(value = "/api/w/maintainPlan")
public class MaintainPlanController {
    @Autowired
    private MaintainPlanService maintainPlanService;

    @ApiOperation("查询所有维护计划")
    @GetMapping
    public ResultListDTO<MaintainPlanDTO> index() {
        final List<MaintainPlanDTO> DTOs = this.maintainPlanService.findAll();
        final ResultListDTO<MaintainPlanDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有维护计划")
    @GetMapping("/p")
    public PageResultDTO<MaintainPlanDTO> search(final UserBean operator, final Pageable pageable,
            final Searchable searchable) {
        if ((operator != null) && CollectionUtils.isNotEmpty(operator.getOrgs())) {
            searchable.put("orgCodes", operator.getOrgs());
        }
        return PageResultDTO.success(this.maintainPlanService.search(pageable, searchable));
    }

    @ApiOperation("创建维护计划")
    @PostMapping
    public ResultDTO<MaintainPlanDTO> create(final UserBean operator, @RequestBody final MaintainPlanDTO dto) {
        if (operator != null) {
            dto.setUserId(operator.getUserId());
            dto.setUserName(operator.getUsername());
        }
        final MaintainPlanDTO result = this.maintainPlanService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改维护计划")
    @PutMapping("/{id}")
    public ResultDTO<MaintainPlanDTO> update(final UserBean operator, @PathVariable final Long id,
            @RequestBody final MaintainPlanDTO dto) {
        final MaintainPlanDTO result = this.maintainPlanService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "变更维护规则状态")
    @PutMapping("/changeStatus/{id}/{flag}")
    public ResultDTO<MaintainPlanDTO> changeStatus(@PathVariable final Long id,
            @PathVariable final String flag) {
        final MaintainPlanDTO result = this.maintainPlanService.changeStatus(id, flag);
        return ResultDTO.success(result);
    }

    @ApiOperation("维护计划详情")
    @GetMapping("/{id}")
    public ResultDTO<MaintainPlanDTO> details(@PathVariable final Long id) {
        final MaintainPlanDTO dto = this.maintainPlanService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除维护计划")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.maintainPlanService.delete(id);
        return ResultDTO.success();
    }

}
